/**
 * 
 */
package com.bj58.data.hadoop.info.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author Administrator
 *
 */
public class Params {

	/*需要分析的单元参数列表*/
	private static Map<String,String> careParamsMap = new HashMap<String,String>();
	
	static
	{
		careParamsMap.put("6821", "513660,513661,513660|513661");
	}
	
	public static List<String> getParamsInCareSpace(Map<String,String> fullParamMap)
	{
		List<String> result = new ArrayList<String>();
		if(fullParamMap == null)
		{
			return null;
		}
		if(fullParamMap.size() > careParamsMap.size())
		{
			Iterator<Entry<String, String>> it = careParamsMap.entrySet().iterator();
			while(it.hasNext())
			{
				Entry<String,String> entry = it.next();
				if(fullParamMap.containsKey(entry.getKey()))/*如果在此信息中存在*/
				{
					String paramVal = fullParamMap.get(entry.getKey());/*513660,513661,513660|513661*/
					/*分析实例值*/
					if(entry.getValue().equals(""))
					{
						result.add(entry.getKey() + "=" +paramVal);
					}
					else
					{
						String[] careVals = entry.getValue().split(",");
						for(String careVal:careVals)
						{
							if(paramVal.equals(careVal))
							{
								result.add(entry.getKey() + "=" +careVal);
							}
						}
					}
				}
			}
		}
		else
		{
			Iterator<Entry<String, String>> it = fullParamMap.entrySet().iterator();
			while(it.hasNext())
			{
				Entry<String,String> entry = it.next();
				if(careParamsMap.containsKey(entry.getKey()))/*如果在此信息中存在*/
				{
					String paramVal = careParamsMap.get(entry.getKey());/*513660,513661,513660|513661*/
					if(paramVal.equals(""))
					{
						result.add(entry.getKey() + "=" +entry.getValue());
					}
					else
					{
						String[] careVals =paramVal.split(",");
						for(String careVal:careVals)
						{
							if(entry.getValue().equals(careVal))
							{
								result.add(entry.getKey() + "=" +careVal);
							}
						}
					}
				}
			}
		}
		return result;
	}
	
	public static Map<String,String> getParamsKV(String param)
	{
		Map<String,String> reusltMap = new HashMap<String,String>();
		@SuppressWarnings("deprecation")
		String unCodeParam = java.net.URLDecoder.decode(param);
		unCodeParam = unCodeParam.replace("\"", "");
		Pattern pattern = Pattern.compile("\\{I:(\\S+?),V:(\\S+?)\\}");
		Matcher matcher = pattern.matcher(unCodeParam);
		while(matcher.find())
		{
			try
			{
				reusltMap.put(matcher.group(1), matcher.group(2));
			}
			catch (Exception e) {
				System.out.println("单元参数处理错误："+matcher.group());
			}
		}
		return reusltMap;
	}
	public static void main(String[] args) {
		
		String dd  = "63418321|24921266|221.201.59.88|1|0|66996368|null|0|true|http://dl.58.com/caishui/|true|2012-10-15 11:14:39.0|1|1|%E5%A4%A7%E8%BF%9E%E4%BC%81%E4%B8%9A%E4%BB%A3%E7%90%86%E8%AE%B0%E5%B8%90|0,534,504|0,147,151,11023|%5B%7B%22I%22%3A5411%2C%22V%22%3A%22520%22%7D%2C%7B%22I%22%3A5923%2C%22V%22%3A%22403359964%22%7D%2C%7B%22I%22%3A2359%2C%22V%22%3A%221%22%7D%2C%7B%22I%22%3A2435%2C%22V%22%3A%22%E5%A4%A7%E8%BF%9E%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%B8%AD%E5%B1%B1%E8%B7%AF117%E5%8F%B7%22%7D%2C%7B%22I%22%3A5332%2C%22V%22%3A%22%E5%A4%A7%E8%BF%9E%E4%BC%81%E4%B8%9A%E4%BB%A3%E7%90%86%E8%AE%B0%E5%B8%90%7C%E4%BB%A3%E7%90%86%E8%AE%B0%E8%B4%A6%7C%E5%A4%A7%E8%BF%9E%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%B8%AD%E5%B1%B1%E8%B7%AF117%E5%8F%B7%22%7D%2C%7B%22I%22%3A5333%2C%22V%22%3A%22%E5%BC%A0%E5%A5%B3%E5%A3%AB%22%7D%2C%7B%22I%22%3A5461%2C%22V%22%3A%22objecttype%3D%25E4%25BB%25A3%25E7%2590%2586%25E8%25AE%25B0%25E8%25B4%25A6%22%7D%2C%7B%22I%22%3A6616%2C%22V%22%3A%221%22%7D%5D|20091208|0|0|Y05,Y0502,504|infolog.v3.1";
		String[] infos = dd.split("\\|");
		String param = infos[InfoField.Params];
		System.out.println("bianma:"+param);
		System.out.println("jiama:"+ java.net.URLDecoder.decode(param));
		String jiema = java.net.URLDecoder.decode(param);
		Pattern pattern = Pattern.compile("\\{\"I\":(\\S+?),\"V\":\"(\\S+?)\"\\}");
		Matcher matcher = pattern.matcher(jiema);
		while(matcher.find())
		{
			System.out.println(matcher.group(2));
		}
		System.out.println(getParamsKV(param));
	}
}
